﻿[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.InputsContainerValueHandler.js",
                "application/x-javascript")]
[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.InputsContainerValueHandler.debug.js",
                "application/x-javascript")]
[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.CheckBoxValueHandler.js",
                "application/x-javascript")]
[assembly:
        System.Web.UI.WebResource("BM.Tools.WebControls.MForm.Controls.ValueHandlers.CheckBoxValueHandler.debug.js",
                "application/x-javascript")]

namespace BM.Tools.WebControls.MForm.Controls.ValueHandlers
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    using AjaxControlToolkit;

    using BM.Tools.WebControls.MForm.Controls.Helper;
    using BM.Tools.WebControls.MForm.Controls.Items;

    /// <summary>
    /// A control that binds the <see cref="LeafItem" /> control with a <see cref="CheckBox" /> control.
    /// </summary>
    [ClientScriptResource("BM.Tools.WebControls.MForm",
            "BM.Tools.WebControls.MForm.Controls.ValueHandlers.InputsContainerValueHandler.js", LoadOrder = 0)]
    [ClientScriptResource("BM.Tools.WebControls.MForm",
            "BM.Tools.WebControls.MForm.Controls.ValueHandlers.CheckBoxValueHandler.js", LoadOrder = 1)]
    public class CheckBoxValueHandler : StandardWebControlValueHandler<CheckBox>
    {
        /// <summary>
        /// Gets or sets CheckBox.
        /// </summary>
        /// <value>
        /// The check box.
        /// </value>
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public CheckBox CheckBox
        {
            get
            {
                return Control;
            }

            set
            {
                Control = value;                
            }
        }

        /// <summary>
        /// Gets or sets OffValue.
        /// </summary>
        /// <value>
        /// The off value.
        /// </value>
        public string OffValue
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets OnValue.
        /// </summary>
        /// <value>
        /// The on value.
        /// </value>
        public string OnValue
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets Value.
        /// </summary>
        public override string Value
        {
            get
            {
                return Control.Checked ? OnValue : OffValue;
            }

            set
            {
                Control.Checked = value == OnValue;
            }
        }

        /// <summary>        
        /// The onValue and offValue custom attributes are sent to enable sending the on and offvalue back to server
        /// </summary>
        protected override void PrepareControl()
        {
            base.PrepareControl();
            Control.Attributes["onValue"] = OnValue;
            Control.Attributes["offValue"] = OffValue;
        }

        /// <summary>
        /// the css class is added for display reasons
        /// </summary>
        /// <returns>
        /// the css classes appropriate for CheckBox value handler
        /// </returns>
        protected override CssClasses PrepareCssClass()
        {
            CssClasses cssClasses = base.PrepareCssClass();
            cssClasses.Add("CheckBox");
            return cssClasses;
        }
    }
}